<!DOCTYPE html>
<html>

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    <link rel="stylesheet" href="{$Think.config.RESOURCEURL}wx_assets/css/common.css{$Think.config.RESOURCE_VER}" onerror="_cdnFallback(this)">
    <link type="text/css" rel="stylesheet" href="{$Think.config.RESOURCEURL}wx_assets/css/zepto.alert.css" />
    <link type="text/css" rel="stylesheet" href="{$Think.config.RESOURCEURL}wx_assets/css/wish/index.css" />
    <script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.1.1.3.min.js{$Think.config.RESOURCE_VER}"></script>
    <script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.alert.js{$Think.config.RESOURCE_VER}"></script>
    <script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/util.js{$Think.config.RESOURCE_VER}"></script>
</head>

<body>
    <div class="page">
        <div class="address-box">
            <ul>
                <li>
                    <div class="fl">
                        收件人：
                        <label id="addr_fullName">{$wish.fullName}</label>
                    </div>
                    <div class="fr">
                        <label id="addr_phone">
                            {$wish.mobile}</label>
                    </div>
                </li>
                <li>
                    收货地址：
                    <label id="addr_detail" style="word-break: break-all;">{$wish.address}</label>
                </li>
            </ul>
        </div>
        <div class="order-box">
            <foreach name="productList" item="vo">
                <dl data-id="{$vo.id}">
                    <dt>
                        <a href='{$Think.config.SERVER_HOST}call.html?customerId={$customerId}&userName={$userName}&module=product&method=getProduct&params=%7B"id"%3A"{$vo.id}"%7D'><img src="{$vo.picUrl}" /></a>
                    </dt>
                    <dd>
                        <div class="pro-name">
                            <a href='{$Think.config.SERVER_HOST}call.html?customerId={$customerId}&userName={$userName}&module=product&method=getProduct&params=%7B"id"%3A"{$vo.id}"%7D'>{$vo.name}</a>
                        </div>
                        <div class="pro-price">
                            <span>￥
                            <notempty name="vo.fixedPrice">
                            <empty name="vo.discountedPrice">
                                <empty name="vo.unitName">
                                     {$vo.fixedPrice}&nbsp;元 
                                </empty>
                                <notempty name="vo.unitName">
                                    {$vo.fixedPrice}&nbsp;元/&nbsp;{$vo.unitName}
                                </notempty>
                            </empty>
                            <notempty name="vo.discountedPrice">
                                <empty name="vo.unitName">
                                    {$vo.discountedPrice}&nbsp;元 
                                </empty>
                                <notempty name="vo.unitName">
                                    {$vo.discountedPrice}&nbsp;元/{$vo.unitName}
                                </notempty>
                            </notempty>
                        </notempty>
                        </span>
                            <br /> ×{$vo.reserveCount}
                            <input type="hidden" id="hide_num_{$vo.id}" value="{$vo.reserveCount}">
                            <input type="hidden" id="hide_price_{$vo.id}" value="{$vo.fixedPrice}">
                            <input type="hidden" id="hide_dis_price_{$vo.id}" value="{$vo.discountedPrice}">
                        </div>
                    </dd>
                </dl>
            </foreach>
        </div>
        <div class="order-info">
            <textarea placeholder="给卖家留言" id="remark"></textarea>
        </div>
        <div class="order-sum">
            商品总价：<span class="chen">￥{$order.amount}&nbsp;元</span>
        </div>
        <div class="order-amount">
            <notempty name="order.cost">
                ￥{$order.price}&nbsp;元&nbsp;+￥&nbsp;{$order.cost}&nbsp;元&nbsp;运费
                <br />
            </notempty>
            <span class="chen">需付：￥{$order.amount}&nbsp;元</span>
        </div>
        <a class="order-btn" id="BtnPay">微信安全支付</a>
        <div class="pay-order-info">
            支付完成后，如需退换货请及时联系卖家
        </div>
        <script type="text/javascript">
        var baseUrl = '{$Think.config.SERVER_HOST}';
        var hrefPath = 'call.html?customerId={$customerId}';
        var basePath = 'call.json?customerId={$customerId}';
        var wish = {
            init: function() {
                this._initEvent();
            },
            submitOrder: function() {
                var me = this;
                var payType="wxpay";
                var params = {
                    "id": "{$order.id}",
                    "remark": "",
                    "sid": -1,
                    "type": "buy",
                    "fullName": "{$order.fullName}",
                    "address": "{$order.address}",
                    "phone": "{$order.phone}",
                    "paytype": "wxpay",
                    "dtype": "delivery"
                }
                var btn = $("#BtnPay");
                if (!btn.hasClass("btn_disabled")) {
                    $.util.pageLoading.show("加载中");
                    btn.addClass("btn_disabled");
                    $.ajax({
                        url: window.basePath,
                        dataType: "json",
                        type: "POST",
                        data: {
                            module: 'product',
                            method: 'submitOrder',
                            params: JSON.stringify(params)
                        },
                        success: function(data) {
                            $.util.pageLoading.hide();
                            if (typeof(data.code) !== "undefined") {
                                btn.removeClass("btn_disabled");
                                $.util.alert(data.msg);
                                return;
                            }
                            var price = data.price;
                            if (payType == "wxpay") {
                                try {
                                    WeixinJSBridge.invoke('getBrandWCPayRequest', data.pkg, function(res) {
                                        if (res.err_msg == 'get_brand_wcpay_request:ok') {
                                            //发送模板消息
                                            var pa = {
                                                "id": "{$order.id}",
                                                "tplid": 1,
                                                "wishId": "{$wishId}"
                                            }
                                            window.location.href = baseUrl + 'call.html?customerId={$customerId}&module=wish&method=successOrder&params=' + JSON.stringify(pa);
                                        } else {
                                            btn.removeClass("btn_disabled");
                                            me.cancelOrder(function() {
                                                if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                                                    return $.util.alert("操作已取消");
                                                } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                                                    return $.util.alert("网络错误发送失败，请稍后再试");
                                                } else if (res.err_msg == 'access_control:not_allow' || res.err_msg == 'system:access_denied') {
                                                    return $.util.alert("商家尚未开通微信支付");
                                                } else {
                                                    return $.util.alert(res.err_msg);
                                                }
                                            });
                                        }
                                    });
                                } catch (e) {
                                    me.cancelOrder(function() {
                                        btn.removeClass("btn_disabled");
                                        return $.util.alert("操作失败，请确保在微信浏览器中操作");
                                    });
                                }
                            }
                        },
                        error: function() {
                            btn.removeClass("btn_disabled");
                            $.util.alert("购买失败");
                        }
                    });
                }
            },
            cancelOrder: function(callback) {
                var btn = $("#BtnPay");
                var orderId="{$order.id}";
                $.ajax({
                    url: window.basePath,
                    dataType: "json",
                    type: "POST",
                    data: {
                        module: 'product',
                        method: 'cancelWxOrder',
                        params: '{"oid":'+orderId+'}'
                    },
                    success: function() {
                        btn.removeClass("btn_disabled");
                        callback && callback();
                    }
                });
            },
            _initEvent: function() {
                var me = this;
                $("#BtnPay").click(function() {
                    me.submitOrder();
                });
            }
        }
        wish.init();
        </script>
</body>

</html>
